package com.fenghen.system.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.fenghen.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.fenghen.system.domain.RiskIdentification;
import com.fenghen.system.mapper.RiskMitigationMapper;
import com.fenghen.system.domain.RiskMitigation;
import com.fenghen.system.service.IRiskMitigationService;
import com.fenghen.common.core.text.Convert;

/**
 * 风险处理与跟踪模块Service业务层处理
 * 
 * @author fenghen
 * @date 2024-11-06
 */
@Service
public class RiskMitigationServiceImpl implements IRiskMitigationService 
{
    @Autowired
    private RiskMitigationMapper riskMitigationMapper;

    /**
     * 查询风险处理与跟踪模块
     * 
     * @param mitigationId 风险处理与跟踪模块主键
     * @return 风险处理与跟踪模块
     */
    @Override
    public RiskMitigation selectRiskMitigationByMitigationId(Long mitigationId)
    {
        return riskMitigationMapper.selectRiskMitigationByMitigationId(mitigationId);
    }

    /**
     * 查询风险处理与跟踪模块列表
     * 
     * @param riskMitigation 风险处理与跟踪模块
     * @return 风险处理与跟踪模块
     */
    @Override
    public List<RiskMitigation> selectRiskMitigationList(RiskMitigation riskMitigation)
    {
        return riskMitigationMapper.selectRiskMitigationList(riskMitigation);
    }

    /**
     * 新增风险处理与跟踪模块
     * 
     * @param riskMitigation 风险处理与跟踪模块
     * @return 结果
     */
    @Transactional
    @Override
    public int insertRiskMitigation(RiskMitigation riskMitigation)
    {
        int rows = riskMitigationMapper.insertRiskMitigation(riskMitigation);
        insertRiskIdentification(riskMitigation);
        return rows;
    }

    /**
     * 修改风险处理与跟踪模块
     * 
     * @param riskMitigation 风险处理与跟踪模块
     * @return 结果
     */
    @Transactional
    @Override
    public int updateRiskMitigation(RiskMitigation riskMitigation)
    {
        riskMitigationMapper.deleteRiskIdentificationByRiskId(riskMitigation.getMitigationId());
        insertRiskIdentification(riskMitigation);
        return riskMitigationMapper.updateRiskMitigation(riskMitigation);
    }

    /**
     * 批量删除风险处理与跟踪模块
     * 
     * @param mitigationIds 需要删除的风险处理与跟踪模块主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteRiskMitigationByMitigationIds(String mitigationIds)
    {
        riskMitigationMapper.deleteRiskIdentificationByRiskIds(Convert.toStrArray(mitigationIds));
        return riskMitigationMapper.deleteRiskMitigationByMitigationIds(Convert.toStrArray(mitigationIds));
    }

    /**
     * 删除风险处理与跟踪模块信息
     * 
     * @param mitigationId 风险处理与跟踪模块主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteRiskMitigationByMitigationId(Long mitigationId)
    {
        riskMitigationMapper.deleteRiskIdentificationByRiskId(mitigationId);
        return riskMitigationMapper.deleteRiskMitigationByMitigationId(mitigationId);
    }

    /**
     * 新增风险识别模块信息
     * 
     * @param riskMitigation 风险处理与跟踪模块对象
     */
    public void insertRiskIdentification(RiskMitigation riskMitigation)
    {
        List<RiskIdentification> riskIdentificationList = riskMitigation.getRiskIdentificationList();
        Long mitigationId = riskMitigation.getMitigationId();
        if (StringUtils.isNotNull(riskIdentificationList))
        {
            List<RiskIdentification> list = new ArrayList<RiskIdentification>();
            for (RiskIdentification riskIdentification : riskIdentificationList)
            {
                riskIdentification.setRiskId(mitigationId);
                list.add(riskIdentification);
            }
            if (list.size() > 0)
            {
                riskMitigationMapper.batchRiskIdentification(list);
            }
        }
    }
}
